/*******************************************************************************
 * Copyright (c) 2008, 2009 Tapestry IDE Developers
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Tapestry IDE Developers - initial API and implementation
 *******************************************************************************/
package com.googlecode.tapestry.ide.ui;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;

import com.googlecode.tapestry.ide.TapestryPlugin;
import com.googlecode.tapestry.ide.internal.core.LogUtils;

/**
 * The Class TapestryImages.
 */
public class TapestryImages {

	/** The Constant ICON_PATH_PREFIX. */
	private static final String ICON_PATH_PREFIX = "icons/full/";

	/** The ICO n_ bas e_ url. */
	private static URL ICON_BASE_URL = null;

	static {
		try {
			ICON_BASE_URL = new URL(TapestryPlugin.getDefault().getBundle()
					.getEntry("/"), ICON_PATH_PREFIX);
		} catch (MalformedURLException e) {
			LogUtils.log(e);
		}
	}

	/** The Constant NAME_PREFIX. */
	private static final String NAME_PREFIX = TapestryPlugin.PLUGIN_ID + '.';

	/** The Constant NAME_PREFIX_LENGTH. */
	private static final int NAME_PREFIX_LENGTH = NAME_PREFIX.length();

	/** The Constant OBJECT. */
	private final static String OBJECT = "obj16/"; // basic colors - size 16x16

	/** The Constant OVR. */
	private final static String OVR = "ovr16/"; // basic colors - size 7x8

	/** The Constant IMG_OBJS_PROJECT. */
	public static final String IMG_OBJS_PROJECT = NAME_PREFIX
			+ "project_obj.gif";

	/** The Constant IMG_OBJS_PAGE. */
	public static final String IMG_OBJS_ITEM = NAME_PREFIX + "item_obj.gif";

	/** The Constant IMG_OBJS_CONTAINER. */
	public static final String IMG_OBJS_CONTAINER = NAME_PREFIX
			+ "container_obj.gif";

	/** The Constant DESC_OBJS_PROJECT. */
	public static final ImageDescriptor DESC_OBJS_PROJECT = createManaged(
			OBJECT, IMG_OBJS_PROJECT);

	/** The Constant DESC_OBJS_PAGE. */
	public static final ImageDescriptor DESC_OBJS_ITEM = createManaged(OBJECT,
			IMG_OBJS_ITEM);

	/** The Constant DESC_OBJS_CONTAINER. */
	public static final ImageDescriptor DESC_OBJS_CONTAINER = createManaged(
			OBJECT, IMG_OBJS_CONTAINER);

	/** The Constant DESC_OVR_TAPESTRY. */
	public static final ImageDescriptor DESC_OVR_TAPESTRY = create(OVR,
			"tapestry_ovr.gif");

	/** A table of all the <code>ImageDescriptor</code>s. */
	private static Map<String, ImageDescriptor> imageDescriptors;

	/**
	 * Initialize image registry.
	 * 
	 * @param registry
	 *            the registry
	 */
	public static void initializeImageRegistry(ImageRegistry registry) {
		for (String key : imageDescriptors.keySet()) {
			registry.put(key, imageDescriptors.get(key));
		}
	}

	/**
	 * Creates the managed.
	 * 
	 * @param prefix
	 *            the prefix
	 * @param name
	 *            the name
	 * 
	 * @return the image descriptor
	 */
	private static ImageDescriptor createManaged(String prefix, String name) {
		try {
			ImageDescriptor result = ImageDescriptor
					.createFromURL(makeIconFileURL(prefix, name
							.substring(NAME_PREFIX_LENGTH)));
			if (imageDescriptors == null) {
				imageDescriptors = new HashMap<String, ImageDescriptor>();
			}
			imageDescriptors.put(name, result);
			return result;
		} catch (MalformedURLException e) {
			LogUtils.log(e);
			return ImageDescriptor.getMissingImageDescriptor();
		}
	}

	/**
	 * Creates the.
	 * 
	 * @param prefix
	 *            the prefix
	 * @param name
	 *            the name
	 * 
	 * @return the image descriptor
	 */
	private static ImageDescriptor create(String prefix, String name) {
		try {
			return ImageDescriptor.createFromURL(makeIconFileURL(prefix, name));
		} catch (MalformedURLException e) {
			LogUtils.log(e);
			return ImageDescriptor.getMissingImageDescriptor();
		}
	}

	/**
	 * Make icon file url.
	 * 
	 * @param prefix
	 *            the prefix
	 * @param name
	 *            the name
	 * 
	 * @return the uRL
	 * 
	 * @throws MalformedURLException
	 *             the malformed url exception
	 */
	private static URL makeIconFileURL(String prefix, String name)
			throws MalformedURLException {
		if (ICON_BASE_URL == null) {
			throw new MalformedURLException();
		}

		StringBuffer buffer = new StringBuffer(prefix);
		buffer.append('/').append(name);
		return new URL(ICON_BASE_URL, buffer.toString());
	}

}
